
/**
 * Copyright (C) FPT - Aptech
 * BillDaoImpl.java - 2012 - khangpm
 */
package dao;

import dto.BillDto;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author khangpm
 */
public class BillDaoImpl extends BaseDao {
    public BillDto getBillByID(int billID) {
        if(getConnect()) {
            StringBuilder sql = new StringBuilder("SELECT * FROM Bill WHERE billID = ?");
            try {
                BillDto dto = new BillDto();
                preparedStatement = connection.prepareStatement(sql.toString());
                preparedStatement.setString(1, String.valueOf(billID));
                
                resultSet = preparedStatement.executeQuery();
                if (resultSet.next()) {
                    dto.setBillID(resultSet.getInt("BillID"));
                    dto.setFrom(resultSet.getInt("From"));
                    dto.setTo(resultSet.getInt("To"));
                    dto.setBillID(resultSet.getInt("TypeBillID"));
                    dto.setTitle(resultSet.getString("Title"));
                    dto.setContent(resultSet.getString("Content"));
                    dto.setAmount(resultSet.getFloat("Amount"));
                    dto.setCreationDate(resultSet.getDate("CreationDate"));
                    dto.setComment(resultSet.getString("Comment"));
                    dto.setStatus(resultSet.getBoolean("status"));
                    
                    return dto;
                }
                
            } catch (SQLException ex) {
                Logger.getLogger(BillDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
        return null;
    }
    
    public ArrayList<BillDto> getListBillDto() {
        ArrayList<BillDto> list = new ArrayList<BillDto>();
        if (getConnect()) {
            StringBuilder sql=new StringBuilder("Select * from Bill");
            try {
                preparedStatement=connection.prepareStatement(sql.toString());
                
                resultSet = preparedStatement.executeQuery();
                while (resultSet.next()) {
                    BillDto dto=new BillDto();
                    dto.setBillID(resultSet.getInt("BillID"));
                    dto.setFrom(resultSet.getInt("From"));
                    dto.setTo(resultSet.getInt("To"));
                    dto.setBillID(resultSet.getInt("TypeBillID"));
                    dto.setTitle(resultSet.getString("Title"));
                    dto.setContent(resultSet.getString("Content"));
                    dto.setAmount(resultSet.getFloat("Amount"));
                    dto.setCreationDate(resultSet.getDate("CreationDate"));
                    dto.setComment(resultSet.getString("Comment"));
                    dto.setStatus(resultSet.getBoolean("status"));
                    
                    list.add(dto);
                }
            } catch (SQLException ex) {
                Logger.getLogger(BillDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
        return list;
    }
}
